﻿using System;
using System.Collections.Generic;
using System.Text;
using Young3.FMSearch.Core.Entities.GameVersions;
using Young3.FMSearch.Core.Managers;
using Young3.FMSearch.Core.Offsets;
using System.Linq;
using Young3.FMSearch.Core.Attributes;

namespace Young3.FMSearch.Core.Entities.InGame
{
    public class PlayerRelations : BaseObject
    {
        public PlayerRelations(int memoryAddress, IVersion version)
            : base(memoryAddress, version)
        { }
        
        public PlayerRelations(int memoryAddress, ArraySegment<byte> originalBytes, IVersion version)
            : base(memoryAddress, originalBytes, version)
        { }

        public RelationObjectType ObjectType
        {
            get
            {
                return (RelationObjectType)ProcessManager.ReadByte(MemoryAddress + PlayerRelationOffsets.RelationObjectType);
            }
            set
            {
                ProcessManager.WriteByte((byte)value, MemoryAddress + PlayerRelationOffsets.RelationObjectType);
            }
        }

        public RelationType RelationType
        {
            get
            {
                return (RelationType)ProcessManager.ReadByte(MemoryAddress + PlayerRelationOffsets.RelationType);
            }
            set
            {
                ProcessManager.WriteByte((byte)value, MemoryAddress + PlayerRelationOffsets.RelationType);
            }
        }

        public RelationType2 RelationType2
        {
            get
            {
                return (RelationType2)ProcessManager.ReadByte(MemoryAddress + PlayerRelationOffsets.RelationType2);
            }
            set
            {
                ProcessManager.WriteByte((byte)value, MemoryAddress + PlayerRelationOffsets.RelationType2);
            }
        }

        [FMEntityAttribute(PlayerRelationOffsets.Value)]
        public Byte Value { get; set; }

        public Club Club
        {
            get
            {
                if (ObjectType == RelationObjectType.Club) return new Club(ProcessManager.ReadInt32(MemoryAddress), Version);
                else return null;
            }
        }

        public Team Team
        {
            get
            {
                if (ObjectType == RelationObjectType.Team) return new Team(ProcessManager.ReadInt32(MemoryAddress), Version);
                else return null;
            }
        }

        public Country Country
        {
            get
            {
                if (ObjectType == RelationObjectType.Country) return new Country(ProcessManager.ReadInt32(MemoryAddress), Version);
                else return null;
            }
        }

        public Player Player
        {
            get
            {
                if (ObjectType == RelationObjectType.Person) return new Player(ProcessManager.ReadInt32(MemoryAddress), Version);
                else return null;
            }
        }

        public Staff Staff
        {
            get
            {
                if (ObjectType == RelationObjectType.Person) return new Staff(ProcessManager.ReadInt32(MemoryAddress) - 0x44, Version);
                else return null;
            }
        }
    }

    public enum RelationObjectType
    {
        Club = 1,
        Country = 2,
        Person = 3,
        Team = 4
    }

    public enum RelationType
    {
        LikesPerson = 1,
        DeslikesPerson = 2,
        LikesClub = 3,
        DeslikesClub = 4,
        RivalClub = 5,
        FormedAtCountry = 70,
        FormedAtClub = 72,
        OtherNationality = 9,
        LeagueRegistration = 25
    }

    public enum RelationType2
    {
        Other = 0x00,
        Legend = 0x4f,
        EligibleForNation = 80
    }
}